package com.ajx.blog.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.ajx.blog.entity.Admin;
import com.ajx.blog.service.api.ArticleService;
import com.ajx.blog.service.api.BlogusersService;
import com.ajx.blog.service.api.ContentTypeService;
import com.ajx.blog.service.api.CourseInfoService;

import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/console")
public class ConsoleController {

    private Logger logger = LoggerFactory.getLogger(ConsoleController.class);

    @Reference(url = "dubbo://localhost:20882")
    private CourseInfoService courseInfoService;

    @Reference(url = "dubbo://localhost:20881")
    private BlogusersService blogusersService;

    @Reference(url = "dubbo://localhost:20883")
    private ArticleService articleService;

    @Reference(url = "dubbo://localhost:20883")
    private ContentTypeService contentTypeService;

    /**
     * @param {HttpSession} session 作用域
     * @return {Object}
     * 
     * @description: 加载控制台页面信息
     */
    @GetMapping
    public Object getConsoleInfo(HttpSession session) {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> dataMap = new HashMap<>();
        logger.info("管理员调用getConsoleInfo()方法");
        // 登录的管理员
        Admin loginAdmin = (Admin) session.getAttribute("loginAdmin");
        // 审核通过文章数量----------有缓存
        Integer passArticleCount = articleService.countByStatus("1");
        // 待审核文章数量----------有缓存
        Integer waitCheckArticleCount = articleService.countByStatus("0");
        // 总文章数量----------有缓存
        Integer allArticleCount = articleService.selectCllArticleCount();
        // 博客用户数量----------有缓存
        Integer bloguserCount = blogusersService.selectBloguserCount();
        // 课程总数量----------有缓存
        Integer allCourseCount = courseInfoService.selectCourseCount();
        // 当月课程与收益对应的map集合----------有缓存
        Map<String, Integer> monthCourseEarningsMap = courseInfoService.courseEarningsMap("thisMonth");
        // 当月课程总收益----------没有缓存
        Integer sumMonthCourseEarnings = 0;
        for (Map.Entry<String, Integer> entry : monthCourseEarningsMap.entrySet()) {
            sumMonthCourseEarnings += entry.getValue();
        }
        // 全部课程与收益对应的map集合----------有缓存
        Map<String, Integer> allCourseEarningsMap = courseInfoService.courseEarningsMap("all");
        // 文章与类型对应的map集合----------有缓存
        Map<String, Integer> articleNumByTypeMap = contentTypeService.selectArticleNumByType();
        // 将信息存在map中
        dataMap.put("passArticleCount", passArticleCount);
        dataMap.put("waitCheckArticleCount", waitCheckArticleCount);
        dataMap.put("allArticleCount", allArticleCount);
        dataMap.put("bloguserCount", bloguserCount);
        dataMap.put("allCourseCount", allCourseCount);
        dataMap.put("sumMonthCourseEarnings", sumMonthCourseEarnings);
        dataMap.put("loginAdmin", loginAdmin);
        dataMap.put("monthCourseEarningsMap", monthCourseEarningsMap);
        dataMap.put("allCourseEarningsMap", allCourseEarningsMap);
        dataMap.put("articleNumByTypeMap", articleNumByTypeMap);
        // 封装返回结果
        returnMap.put("code", 0);
        returnMap.put("data", dataMap);
        returnMap.put("msg", "管理员加载统计信息成功");
        logger.info("管理员加载统计信息成功");
        return returnMap;
    }

}
